package com.system.action.test;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import wzh.TestOrderBean;

import com.system.entity.IFile;
import com.system.main.MySystem;
import com.system.main.SystemServer;
import com.system.main.impl.WindowsSystem;
import com.system.main.impl.WindowsVistor;

@Controller
@RequestMapping("/test")
public class ActionTestTree {
	
	private Map<String, String> map = new HashMap<String, String>();
	
	@RequestMapping("/tree.do")
	public String testTree(HttpServletRequest req){
		
		return "test/testTree";
	}

	@ResponseBody
	@RequestMapping("/query.do")
		public String getRes(HttpServletRequest req,HttpServletResponse res) throws Exception{
		String type = req.getParameter("type");
	
		JSONArray arrs =new JSONArray();
		if("main".equals(type)){
			MySystem ws= SystemServer.getCurrentSystem();
			WindowsVistor wvisitor= new WindowsVistor();
			ws.accept(wvisitor);
			List<IFile> listRootDirs = wvisitor.getFiles();
			for(IFile ifile :listRootDirs){
				JSONObject obj =new JSONObject(ifile);
				arrs.put(obj);
			}
		}else {
			String path = req.getParameter("id");
			MySystem ws= SystemServer.getCurrentSystem();
			List<IFile> listFiles = ws.listFiles(path);
			for(IFile ifile :listFiles){
				JSONObject obj =new JSONObject(ifile);
				arrs.put(obj);
			}
		}
		try {
			res.getWriter().print(arrs.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	@RequestMapping("/desktop.do")
	public String testDeskTop(HttpServletRequest req){
		
		return "system/desktop/TestDesktop";
	}
	
	@ResponseBody
	@RequestMapping("/testform")
	public String testForm (HttpServletRequest req,HttpServletResponse res){
		String name = req.getParameter("id");
		String result = "0";
		if(name.equals("123")){
			result = "1";
		}
		return result;
	}
	
	@RequestMapping("/testaa")
	public String accept (HttpServletRequest req,HttpServletResponse res){
	System.out.println(req.getParameter("id"));
	System.out.println(req.getParameter("name"));
	System.out.println(req.getParameter("moible"));
	System.out.println(req.getParameter("email"));
	return "";
	}
	
	@ResponseBody
	@RequestMapping("/testorder")
	public String testOrder (HttpServletRequest req,HttpServletResponse res) throws Exception{
		
		String index = req.getParameter("index");
		System.out.println(index);
		res.getWriter().print(map.get(index));
	return null;
	}
	
	public void init(){
		for(int j =0;j<3;j++){
			JSONArray arr = new JSONArray();
		for(int i =0;i<5;i++){
			TestOrderBean t = new TestOrderBean();
			t.setState(""+j);
			JSONObject json = new JSONObject(t);
			arr.put(json);
		}
		map.put(j+"", arr.toString());
		}
	}
}
